package cn.gp.usercenter.controller;

import cn.gp.usercenter.constant.UserConstant;
import cn.gp.usercenter.model.domain.User;
import cn.gp.usercenter.service.UserService;
import cn.gp.usercenter.utils.request.UserLoginRequest;
import cn.gp.usercenter.utils.request.UserRegistryRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author He Guoping
 * @Date 2024/11/6 12:50
 * @Version JDK17
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/registry")
    public Long userRegistry(@RequestBody UserRegistryRequest userRegistryRequest) {
        if (userRegistryRequest == null) {
            return null;
        }
        String userAccount = userRegistryRequest.getUserAccount();
        String userPassword = userRegistryRequest.getUserPassword();
        String checkPassword = userRegistryRequest.getCheckPassword();
        if (StringUtils.isAllBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }
        return userService.userRegistry(userAccount, userPassword, checkPassword);
    }

    @PostMapping("/login")
    public User userRegistry(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            return null;
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAllBlank(userAccount, userPassword)) {
            return null;
        }
        return userService.userLogin(userAccount, userPassword, request);
    }

    @GetMapping("/search")
    public List<User> searchUsers(String username, HttpServletRequest request) {
        if (!isAdmin(request)) {
            return null;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isAnyBlank(username)) {
            queryWrapper.like("username", username);
        }
        List<User> userList = userService.list();
        return userList.stream().map(user -> userService.getSafetyUser(user)).toList();
    }

    @DeleteMapping("delete")
    public boolean deleteUser(@RequestBody long id, HttpServletRequest request) {
        if (!isAdmin(request)) {
            return false;
        }
        if (id <= 0) {
            return false;
        }
        return userService.removeById(id);
    }

    /**
     * @Param request
     * @Return boolean
     * @Description 是否为管理员
     * @Author He Guoping
     * @Date 2024/11/6 19:32
     */
    private boolean isAdmin(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(UserConstant.User_LOGIN_STATE);
        User user = (User) userObj;
        if (user == null || user.getUserRole() != UserConstant.ADMIN_ROLE) {
            return false;
        }
        return true;
    }
}
